package com.chenfeng.users.service.impl;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler;

import com.alibaba.fastjson.JSONObject;
import com.chenfeng.common.util.PageInfoUtil;
import com.chenfeng.users.dao.IAAdminsDao;
import com.chenfeng.users.pojo.AAdmins;
import com.chenfeng.users.service.IUserService;
@Service("iUserService")
public class UserServiceImpl implements IUserService
{

	@Resource
	private IAAdminsDao adminsDao;
	/**
	 * 插入一条管理员记录
	 */
	@Override
	public JSONObject insertOneAdmins(AAdmins admins)
	{
		JSONObject resuJson = new JSONObject();
		//注释部分为测试事物回滚是否是正常的
		/*String str =null;
		str.toString();*/
		
		//返回值为影响的行数
		int majorKey = this.adminsDao.insert(admins);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "成功");
			//构建二级json 将主键存储在json对象中
			JSONObject date = new JSONObject();
			date.put("主键", admins.getId());
			resuJson.put("date", date);
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "失败");
		}
		return resuJson;
	}
	/**
	 * 查找一条管理员记录
	 */
	@Override
	public AAdmins findOneAdmins(Map<String, Object> map)
	{
		return this.adminsDao.findOne(map);
	}
	/**
	 * mapper中的sql语句本质上还是分页与不分页（分页的情况下要进行两步走 第一总记录数 第二记录数）
	 * 查找多条管理员记录
	 */
	@Override
	public List<AAdmins> findCondListAdmins(PageInfoUtil pageInfoUtil, Map<String, Object> map)
	{
		List<AAdmins> adminsList = Collections.EMPTY_LIST;
		//分页
		//true跟false在一一起判断
		if(map.get("keyword") != null)
		{
			map.put("keyword", "%"+map.get("keyword")+"%");
		}
		if(pageInfoUtil != null)
		{
			//获取总记录数
			map.put("pageCount", "true");
			adminsList =  this.adminsDao.findCondList(map);
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			//开始分页
			map.put("pageCount", "false");
			map.put("currentRecord", pageInfoUtil.getCurrRecord());
			map.put("pageSize",pageInfoUtil.getPageSize());
			adminsList =  this.adminsDao.findCondList(map);
		}else
		//不分页	
		{
			adminsList = this.adminsDao.findCondList(map);
		}
		return adminsList;
	}
	@Override
	public JSONObject updateOneAdmins(AAdmins admins)
	{
		JSONObject resuJson = new JSONObject();	
		//返回值为影响的行数
		int majorKey = this.adminsDao.update(admins);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "更新成功");
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "更新失败");
		}
		return resuJson;
	}
	@Override
	public JSONObject deleteOneAdmins(Map<String,Object> map)
	{
		JSONObject resuJson = new JSONObject();	
		//返回值为影响的行数
		int majorKey = this.adminsDao.delete(map);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "删除成功");
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "删除失败");
		}
		return resuJson;
	}



}
